package com.hitrust.gs.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import system.LoginContext;
import system.data.ProductTypeBase;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidProvideCodeBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.bean.GSProductTypeManagerBean;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.exception.GSException;

import framework.core.ConstDefine;
import framework.core.DateUtil;

public class GSProductTypeDao extends GSBaseDAO{
	
	private final String insertSql = " insert into t_product_type (" +
	"product_id,"+
	"product_code,"+
	"product_type,"+
	"provider_code,"+
	"remark,"+
	"create_time,"+
	"create_user,"+
	"create_user_id,"+
	"last_update_time,"+
	"last_update_user,"+
	"last_update_user_id,"+
	"is_bid,"+
	"is_balance,"+
	"status,"+
	"id"+
	") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,(select sys_guid() from dual))";	
	
	private final String updateSql = " update t_product_type set " +
	"product_id =?,"+
	"product_code = ?,"+
	"product_type = ?,"+
	"provider_code = ?,"+
	"remark = ?,"+
	"create_time= ?,"+
	"create_user = ?,"+
	"create_user_id = ?,"+
	"last_update_time = ?,"+
	"last_update_user = ?,"+
	"last_update_user_id = ?,"+
	"is_bid = ?,"+
	"is_balance = ?, " +
	"status = ? " +
	"where id = ?";
	
	private final String updateByProductCodeSql = " update t_product_type set " +
	"product_id =?,"+
	"product_code = ?,"+
	"product_type = ?,"+
	"provider_code = ?,"+
	"remark = ?,"+
	"create_time= ?,"+
	"create_user = ?,"+
	"create_user_id = ?,"+
	"last_update_time = ?,"+
	"last_update_user = ?,"+
	"last_update_user_id = ?,"+
	"is_bid = ?,"+
	"is_balance = ?," +
	"status = ? " +
	" where product_code = ?";
	
	/**
	 * 新增产品供应商分类,无连接,不做事务用
	 * @param 实体参数
	 * @return
	 * @throws GSException
	 */
	public boolean insert (GSProductTypeBean vo) throws GSException{
		String tSql = this.insertSql;
		Object[] tobj = new Object[14];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getIsBid());
        tobj[12] = String.valueOf(vo.getIsBalance());
        tobj[13] = String.valueOf(vo.getStatus());
		try {
			return this.executeUpdate(tSql, tobj);
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 新增产品供应商分类，有连接，做事务用
	 * @param 实体参数
	 * @param conn
	 * @return
	 * @throws GSException
	 */
	public boolean insertByConnection (GSProductTypeBean vo,Connection conn) throws GSException{
		String tSql = this.insertSql;
		Object[] tobj = new Object[14];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getIsBid());
        tobj[12] = String.valueOf(vo.getIsBalance());
        tobj[13] = String.valueOf(vo.getStatus());
		try {
			return this.executeUpdate(conn,tSql, tobj);
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 修改产品供应商分类，无连接，非事务用
	 * @param 实体类
	 * @return
	 * @throws GSException
	 */
	public boolean update (GSProductTypeBean vo) throws GSException{
		String tSql = this.updateSql;
		Object[] tobj = new Object[15];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getIsBid());
        tobj[12] = String.valueOf(vo.getIsBalance());
        tobj[13] = String.valueOf(vo.getStatus());
        tobj[14] = vo.getId();
		try {
			return this.executeUpdate(tSql,tobj,null ,"");
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 修改产品供应商分类，有连接，做事务用
	 * @param vo
	 * @param conn
	 * @return
	 * @throws GSException
	 */
	public boolean updateByConnection (GSProductTypeBean vo,Connection conn) throws GSException{
		String tSql = this.updateSql;
		Object[] tobj = new Object[15];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getIsBid());
        tobj[12] = String.valueOf(vo.getIsBalance());
        tobj[13] = String.valueOf(vo.getStatus());
        tobj[14] = vo.getId();
		try {
			return this.executeUpdate(conn,tSql,tobj,null ,"");
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 根据ID查询产品供应商分类
	 * @param Id
	 * @return
	 * @throws GSException
	 */
	public boolean findProductTypeById (String Id) throws GSException{
		String tSql = " select * from t_product_type where id = ?";
		boolean tFlag = false;
		Object[] object = {Id};
		try {
			ArrayList tList = this.executeQuery(new GSProductTypeBean(), tSql, object);
			if(tList.size()>0){
				tFlag = true;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return tFlag;
	}
	
	/**
	 * 根据产品编码查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public GSProductTypeBean findProductTypeByProuductCode (String code) throws GSException{
		String tSql = " select * from t_product_type where product_code = ? and status = 1";
		ArrayList tList ;
		Object[] object = {code};
		try {
			tList = this.executeQuery(new GSProductTypeBean(), tSql, object);
			if(tList.size()>0){
				return (GSProductTypeBean) tList.get(0);
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品编码查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public GSProductTypeBean findProductTypeByProuductCodeNoStauts (String code) throws GSException{
		String tSql = " select * from t_product_type where product_code = ? ";
		ArrayList tList ;
		Object[] object = {code};
		try {
			tList = this.executeQuery(new GSProductTypeBean(), tSql, object);
			if(tList.size()>0){
				return (GSProductTypeBean) tList.get(0);
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	public Map findAllProductType () throws GSException{
		String tSql = " select * from t_product_type where status='1' ";
		ArrayList tList ;
		Object[] object =null;
		 Map map= new HashMap();
		try {
			tList = this.executeQuery(new GSProductTypeBean(), tSql, object);
			if(tList!=null&&tList.size() > 0){
            	for(int i=0;i<tList.size();i++){
            		GSProductTypeBean	bean = (GSProductTypeBean)tList.get(i);
            		map.put(""+bean.getProductCode(),""+bean.getProductType());
            	}
            }
			tList=null;
		} catch (Exception e) {
			 e.printStackTrace();
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return map;
	}
	/**
	 * 根据产品编码查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public GSProductTypeBean findProductTypeByProuductId (String id) throws GSException{
		String tSql = " select * from t_product_type where product_id = ?";
		ArrayList tList ;
		Object[] object = {id};
		try {
			tList = this.executeQuery(new GSProductTypeBean(), tSql, object);
			if(tList.size()>0){
				return (GSProductTypeBean) tList.get(0);
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品类型查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public ArrayList findProductTypeByType (String type) throws GSException{
		String tSql = " select * from t_product_type where product_type = ?";
		ArrayList tList ;
		Object[] object = {type};
		try {
			tList = this.executeQuery(new GSProductTypeBean(), tSql, object);
			if(tList.size()>0){
				return tList;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品类型查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public ArrayList findProviderProductTypeByType (String type) throws GSException{
		String tSql = " select distinct(provider_code) from t_product_type where product_type = ? and status=1";
		ArrayList tList ;
		Object[] object = {type};
		try {
			tList = this.executeQuery(new GSProductBidProvideCodeBean(), tSql, object);
			if(tList.size()>0){
				return tList;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品类型查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public boolean isProductTypeByType (String type) throws GSException{
		String tSql = " select * from t_product_type where product_type = ?";
		ArrayList tList ;
		Object[] object = {type};
		try {
			tList = this.executeQuery(new GSProductTypeBean(), tSql, object);
			if(tList.size()>0){
				return true;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return false;
	}
	
	/**
	 * 根据供应商编码，产品供应商分类删除产品供应商
	 * @param providerCode
	 * @param conn
	 * @param 如果为空或者为NULL为只根据供应商编码删除，
	 * @return
	 * @throws GSException
	 */
	public boolean delete(String providerCode,Connection conn,String type) throws GSException{
		
		String tSql = "";
		boolean flag =false;
		Object[] object = {providerCode};
		if(type.equals("")||type==null){
			tSql = " delete from t_product_type where provider_code = ? ";	
		}else{
			object = new Object[]{providerCode, type};
			tSql = " delete from t_product_type where provider_code = ? and product_type = ?";			
		}	
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	/**
	 * 根据供应商编码，产品供应商分类删除产品供应商,逻辑删除
	 * @param providerCode
	 * @param conn
	 * @param 如果为空或者为NULL为只根据供应商编码删除，
	 * @return
	 * @throws GSException
	 */
	public boolean deleteByStatus(String providerCode,Connection conn,String type) throws GSException{
		
		String tSql = "";
		boolean flag =false;
		Object[] object = {ConstDefine.STR_VALID_NO, providerCode};
		if(type.equals("")||type==null){
			tSql = " update t_product_type set status = ? where provider_code = ? ";	
		}else{
			object = new Object[]{ConstDefine.STR_VALID_NO,providerCode, type};
			tSql = " update t_product_type set status = ? where provider_code = ? and product_type = ?";			
		}	
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean deletePrudctTypeByStatus(String type,Connection conn) throws GSException
	{
		String tSql = " delete from t_product_type where product_type = ?";
		boolean flag =false;
		Object[] object = {type};
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean updatePrudctTypeOrtherByStatus(String type,Connection conn) throws GSException
	{
		String tSql = " update t_product_type set product_type="+GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE+" where product_type = ?";
		boolean flag =false;
		Object[] object = {type};
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean updatePrudctTypeBidByStatus(String bidType,Connection conn) throws GSException
	{
		String tSql = " update t_product_type set is_bid="+GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID+" where product_code in (select product_code from t_product_bid where product_type = ?)";
		boolean flag =false;
		Object[] object = {bidType};
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean deletePrudctTypeByProductCode(String productCode,Connection conn) throws GSException
	{
		String tSql = " delete from t_product_type where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean updatePrudctTypeStatusByProductCode(String productCode) throws GSException
	{
		String tSql = " update t_product_type set status = 0 where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	/**
	 * 根据产品编码，修改对应产品为有效，类型为99
	 * @param productCode
	 * @return
	 * @throws GSException
	 */
	public boolean updatePrudctTypeStatusAndTypeByProductCode(String productCode) throws GSException
	{
		String tSql = " update t_product_type set status = 1,product_type = 99,is_bid=99 where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	
	/**
	 * 根据产品编码，修改对应产品为有效
	 * @param productCode
	 * @return
	 * @throws GSException
	 */
	public boolean updatePrudctTypeIsStatusByProductCode(String productCode) throws GSException
	{
		String tSql = " update t_product_type set status = 1 where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	
	/**
	 * 根据产品编码，修改对应产品的区域分类并将该记录status改为1
	 * @param productCode
	 * @return
	 * @throws GSException
	 */
	public boolean updatePrudctTypeAndStatusByProductCode(String productType,String productCode,String bidStr) throws GSException
	{
		String tSql = " update t_product_type set status = 1,product_type=?,is_bid=? where product_code = ?";
		boolean flag =false;
		Object[] object = {productType,bidStr,productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean updatePrudctTypeStatusByApepCode(String apepCode) throws GSException
	{
		String tSql = " update t_product_type set status = 0 where where APEP_CODE=?";
		boolean flag =false;
		Object[] object = {apepCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	/**
	 * 根据供应商编码插入产品供应商分类
	 * @param providerCode
	 * @param conn
	 * @param context
	 * @param type
	 * @return
	 * @throws GSException
	 */
	public boolean insetByProviderCode (String providerCode,Connection conn,LoginContext context,String type) throws GSException{
		boolean flag = false;
		try {
			GSManufactureDAO dao = new GSManufactureDAO();
			ArrayList list = dao.getManufactureByProviderCode(providerCode);
			if(list.size()>0){
				for (int i = 0; i < list.size(); i++) {
					GSManufactureBean product = (GSManufactureBean) list.get(i);	
					GSProductTypeBean productType = this.findProductTypeByProuductId(product.getId());
	                if(productType ==null){
	                	productType = new GSProductTypeBean();
	                	productType.setCreateTime(new DateUtil().getFullString());
	                	productType.setCreateUser(context.getLoginUserName());
	                	productType.setCreateUserId(context.getLoginUserId());
	                	productType.setIsBalance(1);
	                	productType.setIsBid(1);
	                	productType.setLastUpdateTime(new DateUtil().getFullString());
	                	productType.setLastUpdateUser(context.getLoginUserName());
	                	productType.setLastUpdateUserId(context.getLoginUserId());
	                	productType.setProductCode(product.getGs_concentricity());
	                	productType.setProductId(product.getId());
	                	productType.setProductType(Integer.parseInt(GSBaseConstants.PRODUCT_TYPE4));
	                	productType.setProviderCode(product.getGs_provider_id());
	                	productType.setRemark("");
	                	productType.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
	                    this.insert(productType);
	                }else{
	                	productType.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
	                	productType.setProductId(product.getId());
	                	productType.setProductCode(product.getGs_concentricity());
	                	productType.setProductType(Integer.parseInt(GSBaseConstants.PRODUCT_TYPE4));
	                	productType.setProviderCode(product.getGs_provider_id());
	                	productType.setLastUpdateTime(new DateUtil().getFullString());
	                	productType.setLastUpdateUser(context.getLoginUserName());
	                	productType.setLastUpdateUserId(context.getLoginUserId());
	                	this.update(productType);
	                }
				}
			}
				
		} catch (Exception e) {
			throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
		
		
	}
}
